<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" session="true" %>
<%@ page import="com.controller.LocationManager"  %>
<%@ page import="com.domain.AppUser" %>
<%@ page import="com.domain.Location" %>
<%@ page import="java.util.ArrayList" %>
<%
	HttpSession actualSession = request.getSession(true);
	if(session.getAttribute("LoggedIn") == null){
		response.sendRedirect("/index.jsp");
	}
%>
<!DOCTYPE html>
<!-- InstanceBegin template="/Templates/WhereIWas.dwt.jsp" codeOutsideHTMLIsLocked="false" -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- InstanceBeginEditable name="doctitle" -->
<title>Where I Was - Register Location</title>
<!-- InstanceEndEditable -->
<link type="text/css" rel="stylesheet" href="/stylesheets/main.css"/>
<!-- InstanceBeginEditable name="head" -->
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<script type="text/javascript" src="https://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript" src="javascript/google_maps.js"></script>
<script type="text/javascript" src="javascript/label.js"></script>
<!-- InstanceEndEditable -->
</head>
<!-- InstanceBeginEditable name="bodytag" -->
<body onload="initialize()">
<!-- InstanceEndEditable -->
<h1> Where I Was </h1>

<div id="LoginDiv">
<%
	if(session.getAttribute("LoggedIn") != null){
	boolean isLoggedIn = (Boolean)session.getAttribute("LoggedIn");
		if(isLoggedIn){
%>
	<table>
		<tr><td colspan="3"><%= (String)session.getAttribute("UserName") %></td></tr>
		<tr><td><a href="/register_location.jsp">Register Location</a></td><td><a href="/register_device.jsp">Register a Device</a></td></tr>
		<tr><td colspan="3"><a href="/log_out">Log Out</a></td></tr>
	</table>
<%
		}
	}else{
%>
	<form action="/login" method="post">
    	<table>
        	<tr>
            	<td>Username</td>
                <td><input type="text" name="username" /></td>
                <td>Password</td>
                <td><input type="password" name="password" /></td>
            </tr>
            <tr>
				<td><input type="submit" value="LogIn" /></td>
                <td><input type="reset" value="Restore Fields" /></td>
                <td colspan="2"><a href="/registration.jsp">Register a User</a></td>
            </tr>
        </table>
    </form>
<%
	}
%>
</div>
<div id="Menu">
	<ul>
<%
	if(session.getAttribute("LoggedIn") != null){
%>
    	<li><a href="/view_log.jsp">View Log</a></li>
<%
	}
%>
        <li><a href="/help.jsp">View Where I Was Info</a></li>
    </ul>
</div>
<!-- InstanceBeginEditable name="Body" -->
<script language="javascript" type="text/javascript">
	function isLocationRegistered(registeredValue){
		if(registeredValue){
			alert('Location Registered Successfully');	
		}else{
			alert('Location couldnt be Registered');
		}
	}
	
	function validateRegisterLocationForm(){
		var doubleRegex = /^[-|+]?[0-9]+.[0-9]+$/;
		var registerForm = document.RegisterLocationForm;
		var lat = registerForm.txt_LocationLat.value;
		var validDouble = doubleRegex.test(lat);
		if(!validDouble){
			alert('Not a valid Latitude');
			return;
		}
		var lng = registerForm.txt_LocationLong.value;
		validDouble = doubleRegex.test(lng);
		if(!validDouble){
			alert('Not a valid Longitude');
			return;
		}
		var name = registerForm.txt_LocationName.value;
		if(name == ""){
			alert('Enter a Location Name');
			return;
		}
		var locationRegex = /^[+]?[0-9]+(.[0-9]+)?$/;
		var locationErrorRadius = registerForm.txt_ErrorRadius.value;
		var validRadius = locationRegex.test(locationErrorRadius);
		if(!validRadius){
			alert('Not a valid Location Radius');
			return;
		}
		registerForm.submit();
	}
	
	function toggleShowMapRadius(){
		var b_showMapRadius = document.RegisterLocationForm.chkbx_showRadius.checked;
		var radiusSize = document.RegisterLocationForm.txt_ErrorRadius.value;
		if(b_showMapRadius){
			showMapRadius(radiusSize);
		}else{
			notShowMapRadius();
		}
	}
	
	function toggleShowMarker(checked,position){
		if(checked){
			showLocationFromList(position);
			showRadiusFromList(position);
		}else{
			hideLocationFromList(position);
			hideRadiusFromList(position);
		}
	}
	
	function updateLocation(nameIndex,radiusIndex,locationRealName){
		var locationName = document.registeredLocations[nameIndex].value;
		var locationRadius = document.registeredLocations[radiusIndex].value;
		window.location = "../updateLocation?locationName="+locationName+"&locationRadius="+locationRadius+"&locationRealName="+locationRealName;
	}
	
	function deleteLocation(locationName){
		window.location = "../deleteLocation?locationName="+locationName;
	}
	
</script>
<%
	if(request.getParameter("registeredLocation") != null){
		boolean registered = Boolean.parseBoolean(request.getParameter("registeredLocation"));
		if(registered){
%>
<script language="javascript">
	isLocationRegistered(true);
</script>	
<%		
		}else{
%>
<script language="javascript">
	isLocationRegistered(false);
</script>	
<%
		}
	}
%>
<%
	if(request.getParameter("alreadyExists") != null){
%>
<script language="javascript">
	alert('Location already exists');
</script>	
<%
	}
%>
<div id="LocationForm">
	<form method="post" name="RegisterLocationForm" action="/locationRegister">
    	<table>
        	<tr><td>Location Name</td><td><input type="text" name="txt_LocationName" onKeyUp="changeMarkerTitle()" /></td></tr>
            <tr><td>Location Latitude</td><td><input type="text" name="txt_LocationLat" /></td></tr>
            <tr><td>Location Longitude</td><td><input type="text" name="txt_LocationLong"  /></td></tr>
            <tr><td>Location Error Radius</td><td><input type="text" name="txt_ErrorRadius" onKeyUp="toggleShowMapRadius()"/></td></tr>
            <tr><td>Show Error Radius</td><td><input name="chkbx_showRadius" type="checkbox" onClick="toggleShowMapRadius()"/></td></tr>
            <!--<tr><td><input type="hidden" value="<%= session.getAttribute("UserName") %>"/></td></tr>-->
            <tr><td><input type="button" value="Register Location" onClick="validateRegisterLocationForm()" /></td><td><input type="reset" value="Clear Form" /></td></tr>
        </table>
    </form>
</div>

<div id="registeredLocation">
<form name="registeredLocations">
<table>
<tr><td colspan="4">Registered Locations</td></tr>
<tr><td>Location</td><td>Latitude</td><td>Longitude</td><td>Radius</td><td colspan="1">Show Marker</td></tr>
<%
	LocationManager locationManager = new LocationManager();
	String userName = (String)session.getAttribute("UserName");
	AppUser tempUser = new AppUser(userName,null,null,null);
	ArrayList<Location> registeredLocations = locationManager.getLocationsList(tempUser);
	for(int i=0;i<registeredLocations.size();i++){
		Location registeredLocation = registeredLocations.get(i);
		String locationName = registeredLocation.getLocationName();
		double lat = registeredLocation.getLatitude();
		double lng = registeredLocation.getLongitude();
		double errorRadius = registeredLocation.getErrorRadius();
%>
<script language="javascript">
	addLocationToList(<%= lat %>,<%= lng %>,<%= i %>);
	addLocationRadiusToList(<%= lat %>,<%= lng %>, <%= errorRadius %>,<%= i %>);
</script>
<%
	String chkBoxName = "checkBox" + i;
	int nameIndex = i * 4;
	int radiusIndex = nameIndex + 1;
%>
<tr><td><input type="text" name="txt_locationName<%=i%>" value="<%= locationName %>"/></td><td><%= lat %></td><td><%= lng %></td><td><input type="text" name="txt_locationRadius<%=i%>" value="<%=errorRadius%>"/></td><td><input name="<%=chkBoxName%>" type="checkbox" onClick="toggleShowMarker(document.registeredLocations.<%=chkBoxName%>.checked,<%=i%>)"/><td><input type="button" value="Update Location" onClick="updateLocation(<%=nameIndex%>,<%=radiusIndex%>,'<%=locationName%>')"/></td><td><input type="button" value="Delete Location" onClick="deleteLocation('<%=locationName%>')"/></td></tr>
<%
	}
%>
</table>
</form>
</div>

<div id="map_canvas">
</div>
<!-- InstanceEndEditable -->
<div id="PoweredByGoogle">
<img src="https://developers.google.com/appengine/images/appengine-silver-120x30.gif" 
alt="Powered by Google App Engine" />
<div id="PoweredByAndroid">
<img src="http://1.bp.blogspot.com/_1i7EX7a2ELY/THKCOo4GjQI/AAAAAAAAATg/N-j9SfzyGjk/s200/500px-android-logosvg.png" alt="Powered by Android" height="50" width="50"/>
</div>
</div>
</body>
<!-- InstanceEnd --></html>